package xiao.ke.serviceinvoker1.service;/**
 * @author XuXiaoK
 * @create 2020-01-21 22:52
 */

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Component;
import xiao.ke.serviceinvoker1.entity.Person;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @Author XiaoKe
 * @Date 2020/1/21 22:52
 * @Description TODO
 */
@Component
public class CollapseService {

    // 配置收集1秒内的请求
    @HystrixCollapser(batchMethod = "getPersons",collapserProperties =
            {
                    @HystrixProperty(name = "timerDelayInMilliseconds", value = "1000")
            })
    public Future<Person> getSinglePerson(Integer id) {
        System.out.println("执行单个获取的方法");
        return null;
    }

    @HystrixCommand
    public List<Person> getPersons(List<Integer> ids) {
        System.out.println("收集请求，数量：" + ids.size());
        List<Person> people = new ArrayList<>();
        for (Integer id : ids) {
            Person person = Person.builder().id(id).name("xiaoke").build();
            people.add(person);
        }
        return people;
    }
}
